package com.djakapm.markdown.editor.component.listener;

import com.djakapm.markdown.editor.component.EditorComponentPresenter;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MarkdownTextEditorContextMenuListener extends MouseAdapter {
  JPopupMenu popup;

    public MarkdownTextEditorContextMenuListener(EditorComponentPresenter presenter) {
      this.popup = presenter.getView().getMarkdownTextEditor().getPopupMenu();
    }

    public void mousePressed(MouseEvent e) {
      maybeShowPopup(e);
    }

    public void mouseReleased(MouseEvent e) {
      maybeShowPopup(e);
    }

    private void maybeShowPopup(MouseEvent e) {
      if (e.isPopupTrigger()) {
        this.popup.show(e.getComponent(), e.getX(), e.getY());
      }
    }
}
